package com.example.e_book.DAO;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.e_book.DBUtil.DBHelper;
import com.example.e_book.entity.Book;
import com.example.e_book.entity.User;

import java.util.ArrayList;
import java.util.List;

public class BookshelfDAO {
    private final DBHelper dbHelper;
    private User user;

    public BookshelfDAO(Context context,User user) {
        dbHelper = new DBHelper(context);
        this.user=user;
    }

    public long addBookToShelf(int bookId, int userId) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_BOOKSHELF_BOOK_ID, bookId);
        values.put(DBHelper.COLUMN_BOOKSHELF_USER_ID, userId);
        return db.insert(DBHelper.TABLE_BOOKSHELF, null, values);
    }

    public int removeBookFromShelf(int bookId, int userId) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        return db.delete(DBHelper.TABLE_BOOKSHELF,
                DBHelper.COLUMN_BOOKSHELF_BOOK_ID + "=? AND " + DBHelper.COLUMN_BOOKSHELF_USER_ID + "=?",
                new String[]{String.valueOf(bookId), String.valueOf(userId)});
    }

    public Cursor isBookInShelf(int bookId, int userId) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] columns = { DBHelper.COLUMN_BOOKSHELF_BOOK_ID, DBHelper.COLUMN_BOOKSHELF_USER_ID };
        String selection = DBHelper.COLUMN_BOOKSHELF_BOOK_ID + "=? AND " + DBHelper.COLUMN_BOOKSHELF_USER_ID + "=?";
        String[] selectionArgs = { String.valueOf(bookId), String.valueOf(userId) };
        return db.query(DBHelper.TABLE_BOOKSHELF, columns, selection, selectionArgs, null, null, null);
    }

    public List<Book> getBooksByUserShelf(int userId) {
        List<Book> books = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        // 首先查询书架中的书籍ID
        String[] shelfColumns = { DBHelper.COLUMN_BOOKSHELF_BOOK_ID };
        String shelfSelection = DBHelper.COLUMN_BOOKSHELF_USER_ID + "=?";
        String[] shelfSelectionArgs = { String.valueOf(userId) };
        Cursor shelfCursor = db.query(DBHelper.TABLE_BOOKSHELF, shelfColumns, shelfSelection, shelfSelectionArgs, null, null, null);

        if (shelfCursor.moveToFirst()) {
            do {
                @SuppressLint("Range") int bookId = shelfCursor.getInt(shelfCursor.getColumnIndex(DBHelper.COLUMN_BOOKSHELF_BOOK_ID));
                Book book = getBookById(bookId, db); // 从书籍表中获取书籍详细信息
                if (book != null) {
                    books.add(book);
                }
            } while (shelfCursor.moveToNext());
            shelfCursor.close();
        }
        db.close();
        return books;
    }

    @SuppressLint("Range")
    private Book getBookById(int bookId, SQLiteDatabase db) {
        String[] bookColumns = {
                DBHelper.COLUMN_BOOK_ID,
                DBHelper.COLUMN_BOOK_TITLE,
                DBHelper.COLUMN_BOOK_AUTHOR,
                DBHelper.COLUMN_CATEGORY,
                DBHelper.COLUMN_BOOK_MAIN_CONTENT,
                DBHelper.COLUMN_BOOK_COVER_IMAGE
        };
        String bookSelection = DBHelper.COLUMN_BOOK_ID + "=?";
        String[] bookSelectionArgs = { String.valueOf(bookId) };
        Cursor bookCursor = db.query(DBHelper.TABLE_BOOKS, bookColumns, bookSelection, bookSelectionArgs, null, null, null);
        Book book = null;
        if (bookCursor != null && bookCursor.moveToFirst()) {
            book = new Book();
            book.setBookId(bookCursor.getInt(bookCursor.getColumnIndex(DBHelper.COLUMN_BOOK_ID)));
            book.setTitle(bookCursor.getString(bookCursor.getColumnIndex(DBHelper.COLUMN_BOOK_TITLE)));
            book.setAuthor(bookCursor.getString(bookCursor.getColumnIndex(DBHelper.COLUMN_BOOK_AUTHOR)));
            book.setCategory(bookCursor.getString(bookCursor.getColumnIndex(DBHelper.COLUMN_CATEGORY)));
            book.setContentPath(bookCursor.getString(bookCursor.getColumnIndex(DBHelper.COLUMN_BOOK_MAIN_CONTENT)));
            book.setCoverImagePath(bookCursor.getString(bookCursor.getColumnIndex(DBHelper.COLUMN_BOOK_COVER_IMAGE)));
            bookCursor.close();
        }
        return book;
    }
}